package com.itheima.entry.dao.impl;

import com.itheima.entry.dao.IUserDao;
import com.itheima.entry.dao.SaveUser;
import com.itheima.entry.domain.UserInfo;


import java.io.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.ArrayList;


public class UserDaoImpl implements IUserDao {

    //注册的秘钥
    private static final String adminKey = "1";
    private static final String superKey = "2";
    private static ArrayList<UserInfo> listUser = new ArrayList<>();
    private static Map<String, String> userMap = new HashMap<>();


    static {


        ObjectInputStream obReader = null;
        ArrayList<UserInfo> list = null;

        try {
            obReader = new ObjectInputStream(new FileInputStream("D:\\heima_system\\backup\\userList.txt"));
            list = (ArrayList<UserInfo>) obReader.readObject();


            for (UserInfo user : list) {
                listUser.add(user);
                userMap.put(user.getUsername(), user.getPassword());
            }
            show(listUser);//打印用户信息

        } catch (ClassNotFoundException | IOException e) {
//            e.printStackTrace();
            System.out.println("用户暂无信息暂无数据");
        } finally {


            if (obReader != null) {
                try {
                    obReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void show(ArrayList<UserInfo> list){
        System.out.println("用户名\t密码\t\t\t邮箱\t\t\t电话号码\t\t\t登录时间\t退出时间\t注册时间\t导出文件时间");
        for (UserInfo user : list) {
            System.out.println(user.getUsername()+"\t"+
                                user.getPassword()+"\t"+
                    user.getEmail()+"\t"+
                    user.getPhone()+"\t"+
                    user.getLoginDate()+"\t"+
                    user.getExitDate()+"\t"+
                    user.getRegisterDate()+
                    user.getExportDateList());
        }
    }

    //判断用户名密码是否正确
    @Override
    public boolean login(String username, String password) {


        if (isCorrect(username, password)) {


            //给用户记录登陆时间
            listUser.get(getIndex(username)).setLoginDate(LocalDateTime.now());
            //开启线程,实时记录退出时间


            //实时保存用户信息
            Thread savingUser = new Thread(new SaveUser(listUser, getUser(username)));
            savingUser.start();


            return true;
        }

        return false;

    }

    @Override
    public boolean register(UserInfo user) {

        listUser.add(user);
        userMap.put(user.getUsername(), user.getPassword());
//        System.out.println(listUser);
        return true;
    }


    @Override
    public String getLastDate(String username) {
        return null;
    }

    @Override
    public boolean isEmailExist(String email) {
        for (UserInfo user : listUser) {
            if (email.equals(user.getEmail())) {
                return true;
            }
        }
        return false;
    }


    @Override
    public void ChangePassword(String username, String newPassword) {
        listUser.get(getIndex(username)).setPassword(newPassword);
        userMap.put(username, newPassword);
    }

    //对密码的正确性进行判断
    @Override
    public boolean isCorrect(String username, String password) {

        if (userMap.get(username) != null && userMap.get(username).equals(password)) {
            return true;
        }
        return false;
    }


    @Override
    public boolean readLogs() {
        return false;
    }

    @Override
    public boolean isAdmin(String key) {
        return key.equals(adminKey);
    }

    @Override
    public boolean isUserExist(String username) {

        if (getIndex(username) != -1) {
            return true;
        }
        return false;
    }

    @Override
    public UserInfo getUser(String username) {

        return listUser.get(getIndex(username));
    }

    @Override
    public String getSuperKey() {
        return superKey;
    }

    @Override
    public ArrayList<UserInfo> getAllUsers() {
        return listUser;
    }

    @Override
    public boolean removeUser(String name) {

        if(isUserExist(name)){
            listUser.remove(getUser(name));
            userMap.remove(name);
            return true;
        }
        return false;
    }

    //根据名字找索引
    public int getIndex(String name) {
        if (listUser != null) {
            for (int i = 0; i < listUser.size(); i++) {
                if (listUser.get(i) != null && listUser.get(i).getUsername().equals(name)) {
                    return i;
                }
            }
        }
        return -1;
    }


}
